﻿// ***********************************************************************
// Assembly         : STX.Admin
// Author           : seven.x
// Created          : 12-26-2013
//
// Last Modified By : seven.x
// Last Modified On : 12-26-2013
// ***********************************************************************
// <copyright file="LoginController.cs" company="驰创数码">
//     Copyright (c) 驰创数码. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.Web.Mvc;

using STX.Web.Framework;
using STX.Lib.Security;
using STX.Service;
using STX.Lib.Utils;

namespace STX.Admin.Controllers
{
    /// <summary>
    /// Class LoginController.
    /// </summary>
    public class LoginController : BaseController
    {
        /// <summary>
        /// Gets or sets the safe code provider.
        /// </summary>
        /// <value>The safe code provider.</value>
        public ISafeCodeProvider SafeCodeProvider { get; set; }
        /// <summary>
        /// Gets or sets the administrator service.
        /// </summary>
        /// <value>The administrator service.</value>
        public TabAdministratorService AdministratorService { get; set; }

        /// <summary>
        /// Indexes this instance.
        /// </summary>
        /// <returns>ActionResult.</returns>
        public ActionResult Index()
        {
            if (AdminContext.AdminIsLogin)
            {
               return RedirectToAction("index", "home");
            }
            ViewBag.SiteName = STXConfig.GetValue("config/app/name");
            
            return View();
        }

        /// <summary>
        /// Indexes the specified user name.
        /// </summary>
        /// <param name="userName">The user name.</param>
        /// <param name="password">The password.</param>
        /// <param name="safe code">The safe code.</param>
        /// <returns>JsonResult.</returns>
        /// <exception cref="System.Exception">验证码错误</exception>
        [HttpPost]
        public JsonResult Index(string userName,string password,string safeCode)
        {
            if (!SafeCodeProvider.ValidSafeCode(Session.SessionID, safeCode))
            {
                throw new Exception("验证码错误");
            }

            AdministratorService.CheckLoginUserError(userName, error => {
                throw new Exception(error);
            });

            dynamic result;
            if (AdministratorService.Login(userName, password, model => {

                if (model == null) {
                    throw new Exception("找不到该用户");
                }
                model.Password = password;
                if (HttpContext.Session != null)
                    model.Token = HashEncrypt.MD5(string.Format("{0}&uL%&*)()59&^&_)?:>>:{1}", model.UserName, HttpContext.Session.SessionID));
                model.ErrorCount = 0;
                model.LastErrorDateTime = DateTime.Now.AddYears(-1);
                model.LastLoginTime = DateTime.Now;
                model.LoginTimes++;
                model.LastLoginIp = "127.0.0.1";
                AdministratorService.Update(model);

                AdminContext.SetTicket(model);

            }))
            {
                result = new { success = true };
            }
            else
            {
                result = new { success = false, errmsg = "用户名密码错误" };
            }

            return new JsonResult
            {
                Data = result
            };
        }

    }
}
